﻿using OpenTK.Graphics.OpenGL4;

namespace TrinityEngine
{
    public struct VertexBufferElement
    {
        public int count;

        public VertexAttribPointerType type;

        public bool normalized;

        public VertexBufferElement(int count, VertexAttribPointerType type, bool normalized)
        {
            this.count = count;
            this.type = type;
            this.normalized = normalized;
        }

        public static int GetSizeOfType(VertexAttribPointerType type)
        {
            switch (type)
            {
                case VertexAttribPointerType.Int:
                    return sizeof(int);
                case VertexAttribPointerType.Float:
                    return sizeof(float);
                case VertexAttribPointerType.UnsignedInt:
                    return sizeof(uint);
                case VertexAttribPointerType.UnsignedByte:
                    return sizeof(byte);
            }

            return 0;
        }
    }

    public class VertexBufferLayout
    {
        private List<VertexBufferElement> _elements = new List<VertexBufferElement>();

        private int _stride = 0;

        public void PushInt(int count)
        {
            _elements.Add(new VertexBufferElement(count, VertexAttribPointerType.Int, false));
            _stride += count * VertexBufferElement.GetSizeOfType(VertexAttribPointerType.Int);
        }

        public void PushFloat(int count)
        {
            _elements.Add(new VertexBufferElement(count, VertexAttribPointerType.Float, false));
            _stride += count * VertexBufferElement.GetSizeOfType(VertexAttribPointerType.Float);
        }

        public void PushUnsignedInt(int count)
        {
            _elements.Add(new VertexBufferElement(count, VertexAttribPointerType.UnsignedInt, false));
            _stride += count * VertexBufferElement.GetSizeOfType(VertexAttribPointerType.UnsignedInt);
        }

        public void PushUnsignedByte(int count)
        {
            _elements.Add(new VertexBufferElement(count, VertexAttribPointerType.UnsignedByte, true));
            _stride += count * VertexBufferElement.GetSizeOfType(VertexAttribPointerType.UnsignedByte);
        }

        public List<VertexBufferElement> GetElements()
        {
            return _elements;
        }

        public int GetStride()
        {
            return _stride;
        }
    }
}
